﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DreamIn.Structures.ServerDatabase.Entities;
using MySql.Data.MySqlClient;

namespace DreamIn.Structures.ServerDatabase.DataAccessLayer
{
    class ShareDAL
    {
         public List<ShareEntity> GetShares()
        {
            MySqlConnection connection = DALHelper.Connection;
            try
            {
               MySqlCommand command = connection.CreateCommand();
               MySqlDataReader Reader;

               command.CommandText = "select id_share,id_picture,id_user from shares";
               connection.Open();
               Reader = command.ExecuteReader();
               List<ShareEntity> result=new List<ShareEntity>();
               while (Reader.Read()) {
                   result.Add(new ShareEntity() { id_share = Reader.GetInt16(0), id_picture=Reader.GetInt16(1), id_user=Reader.GetInt16(2)});
               }
               Reader.Close();
               return result;
            }
            finally
            {
                connection.Close();
            }
        }

         public void AddShare(int id_picture, int id_user)
         {
             MySqlConnection connection = DALHelper.Connection;
             try
             {
                 MySqlCommand command = connection.CreateCommand();

                 connection.Open();
                 command.CommandText = "INSERT INTO shares (id_picture, id_user) VALUES" + "('" + id_picture + id_user + "')";
                 command.ExecuteNonQuery();
             }
             finally
             {
                 connection.Close();
             }
         }

         public void DeleteShare(ShareEntity share)
         {
             MySqlConnection connection = DALHelper.Connection;
             try
             {
                 MySqlCommand command = connection.CreateCommand();

                 connection.Open();
                 command.CommandText = "DELETE FROM shares WHERE id_share=" + share.id_share;
                 command.ExecuteNonQuery();
             }
             finally
             {
                 connection.Close();
             }
         }

         public void UpdateShare(int id_share, int id_picture, int id_user)
         {
             MySqlConnection connection = DALHelper.Connection;
             try
             {
                 MySqlCommand command = connection.CreateCommand();

                 connection.Open();
                 command.CommandText = "UPDATE shares SET id_picture='" + id_picture + "', id_user='" + id_user + "' WHERE id_share=" + id_share;
                 command.ExecuteNonQuery();
             }
             finally
             {
                 connection.Close();
             }
         }
    }
 }
